#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <jsoncpp/json/json.h>

#define SPACE " "
#define SPACELEN strlen(SPACE)

class Request
{
public:
#define MYSTYLE

    Request()
        : _x(0), _y(0), _op('0')
    {
    }

    Request(int x, int y, char op)
        : _x(x), _y(y), _op(op)
    {
    }

    std::string Serialization()
    {
#ifdef MYSTYLE
        // 返回一个_x _op _y的字符串
        std::string res;
        res += std::to_string(_x);
        res += SPACE;
        res += _op;
        res += SPACE;
        res += std::to_string(_y);

        return res;
#else
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["op"] = _op;
        Json::StyledWriter write;
        return write.write(root);

#endif
    }

    bool Deserialization(const std::string &str)
    {
#ifdef MYSTYLE
        size_t left = str.find(SPACE);
        if (left == std::string::npos)
        {
            return false;
        }
        size_t right = str.rfind(SPACE);
        if (right == std::string::npos || left >= right)
        {
            return false;
        }

        std::string _number1 = str.substr(0, left);
        std::string _operator = str.substr(left + SPACELEN, right - left - SPACELEN);
        std::string _number2 = str.substr(right + SPACELEN, std::string::npos);

        _x = atoi(_number1.c_str());
        _op = *(_operator.c_str());
        _y = atoi(_number2.c_str());
#else
        Json::Value root;
        Json::Reader reader;
        reader.parse(str, root);

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _op = root["op"].asInt();
#endif
    }

    int _x;
    int _y;
    char _op;
};

class Respond
{
public:
    Respond()
        : _result(0), _code(0)
    {
    }

    Respond(int result, int code)
        : _result(result), _code(code)
    {
    }

    std::string Serialization()
    {
#ifdef MYSTYLE
        // 返回一个_result _code的字符串
        std::string res;
        res += std::to_string(_result);
        res += SPACE;
        res += std::to_string(_code);

        return res;
#else
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        Json::StyledWriter writer;
        return writer.write(root);
#endif
    }

    bool Deserialization(const std::string &str)
    {
#ifdef MYSTYLE
        size_t pos = str.find(SPACE);
        if (pos == std::string::npos)
        {
            return false;
        }

        std::string _number1 = str.substr(0, pos);
        std::string _number2 = str.substr(pos + SPACELEN, std::string::npos);

        _result = atoi(_number1.c_str());
        _code = atoi(_number2.c_str());
#else
        Json::Value root;
        Json::Reader reader;
        reader.parse(str, root);

        _result = root["result"].asInt();
        _code = root["code"].asInt();
#endif
    }

    int _result;
    int _code;
};